﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace TestApplication
{
    public partial class FormContainer : Form
    {
        SettingsForm mySettingsForm;
        TestApplication.FormTest myTestForm;
        protected String currentEbmtDbConnectionString;
        protected String currentMappingInfoConnectionString;
        protected String currentAgnisStagingConnectionString;
        public FormContainer()
        {
            InitializeComponent();
        }


        #region SETTING functions

        private bool TestConnectionString(String connString)
        {
            try
            {
                SqlConnection myTestConnection = new SqlConnection(connString);
                myTestConnection.Open();                  
                myTestConnection.Close();          
                return true;                            
            }
            catch (Exception)
            {
                return false;
            }
        }

        
        public void LoadConfigurationSettings()
        {
            System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

            System.Configuration.KeyValueConfigurationElement myEbmtDbKey =
                config.AppSettings.Settings["userDefinedEBMTDbConnectionString"];
            System.Configuration.KeyValueConfigurationElement myMappingInfoKey =
                config.AppSettings.Settings["userDefinedMappingInfoConnectionString"];
            System.Configuration.KeyValueConfigurationElement myAgnisStagingKey =
               config.AppSettings.Settings["userDefinedAgnisStagingConnectionString"];
            if (myEbmtDbKey.Value != "" && myMappingInfoKey.Value != "" && myAgnisStagingKey.Value != "")
            {
                ReadUserDefinedConnectionStrings(myEbmtDbKey, myMappingInfoKey, myAgnisStagingKey);
            }
            else
            {
                ReadDefaultConnectionStrings();
            }
        } 

        private void ReadDefaultConnectionStrings()
        {
            ConnectionStringSettingsCollection connections =
                ConfigurationManager.ConnectionStrings;

            if (connections.Count != 0)
            {
                // Get the collection elements.
                foreach (ConnectionStringSettings connection in
                 connections)
                {
                    string name = connection.Name;
                    //if (connection.Name.IndexOf("EBMTDb") > -1)
                    if (connection.Name.IndexOf("EBMTD") > -1) //altered by RG 27102011
                    {
                        currentEbmtDbConnectionString = connection.ConnectionString;
                    }
                    if (connection.Name.IndexOf("Mapping") > -1)
                    {
                        currentMappingInfoConnectionString = connection.ConnectionString;
                    }
                    if (connection.Name.IndexOf("AGNIS_FORMS") > -1)
                    {
                        currentAgnisStagingConnectionString = connection.ConnectionString;
                    }
                }
            }
        }

        private void ReadUserDefinedConnectionStrings(System.Configuration.KeyValueConfigurationElement ebmtDbKey,
            System.Configuration.KeyValueConfigurationElement mappingInfoKey, System.Configuration.KeyValueConfigurationElement agnisStagingKey)
        {
            currentEbmtDbConnectionString = ebmtDbKey.Value;
            
            currentMappingInfoConnectionString = mappingInfoKey.Value;
            
            currentAgnisStagingConnectionString = agnisStagingKey.Value;
        }

        #endregion

        #region EVENT functions

        private void FormContainer_Load(object sender, EventArgs e)
        {
            //load and check my settings
           
            LoadConfigurationSettings();
            //If settings fails, then open my setting forms
            if (TestConnectionString(currentEbmtDbConnectionString)
                && TestConnectionString(currentMappingInfoConnectionString)
                && TestConnectionString(currentAgnisStagingConnectionString))
            {
                OpenTestForm();
            }
            //else open my test window.
            else
            {
                OpenSettingsForm();
            }
        }
        private void btnOpenSettingsForm_Click(object sender, EventArgs e)
        {
            //if test form is open, close it before opening setting form
            if (myTestForm != null && !myTestForm.IsDisposed)
            {
                myTestForm.Close();
            }
            OpenSettingsForm();
        }
        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if setting form is open, close it before opening test form
            if (mySettingsForm != null && !mySettingsForm.IsDisposed)
            {
                mySettingsForm.Close();
            }
            OpenTestForm();
        }

        #endregion

        #region CALL BY EVENT functions

        public void OpenSettingsForm()
        {
            if (mySettingsForm != null && !mySettingsForm.IsDisposed)
            {
                mySettingsForm.MdiParent = this;
                mySettingsForm.SetFormContainer(this);
                mySettingsForm.Dock = DockStyle.Fill;
                mySettingsForm.Show();
            }
            else
            {
                mySettingsForm = new SettingsForm();
                mySettingsForm.SetFormContainer(this);
                mySettingsForm.MdiParent = this;
                mySettingsForm.Dock = DockStyle.Fill;
                mySettingsForm.Show();
            }

        }

        public void OpenTestForm()
        {
            if (myTestForm != null && !myTestForm.IsDisposed)
            {
                myTestForm.MdiParent = this;
                myTestForm.Dock = DockStyle.Fill;
                myTestForm.SetCurrentEbmtDbConnectionString(this.currentEbmtDbConnectionString);
                myTestForm.SetCurrentMappingInfoConnectionString(this.currentMappingInfoConnectionString);
                myTestForm.SetCurrentAgnisStagingConnectionString(this.currentAgnisStagingConnectionString);
                myTestForm.Show();
            }
            else
            {
                myTestForm = new FormTest();
                myTestForm.MdiParent = this;
                myTestForm.Dock = DockStyle.Fill;
                myTestForm.SetCurrentEbmtDbConnectionString(this.currentEbmtDbConnectionString);
                myTestForm.SetCurrentMappingInfoConnectionString(this.currentMappingInfoConnectionString);
                myTestForm.SetCurrentAgnisStagingConnectionString(this.currentAgnisStagingConnectionString);
                myTestForm.Show();
            }
        }

        #endregion

       

    }
}
